﻿(function () {
	rf.layout =
		{
			settings: {
				urls: {
				},
				vars: {
					images: null,
					imagePrev: null,
					rndPrev: null
				},
				elements: {
					bgImage: '#bg',
					mainMenuItem: '.main_menu .menu li a',
					panelMenuItem: '.user-panel .menu li a',
					lightbox: '[data-rel="lightbox"]',
					grid: '#Grid',
					userPanel: '.user-panel',
					mainMenu: '.main_menu'
				}
			},

			init: function (settings) {
				$.extend(true, rf.layout.settings, settings);

				var $bg = $(rf.layout.settings.elements.bgImage);

				if ($bg) {
					$bg.load(function () {
						$bg.fadeTo(2000, 1);
						setTimeout(function () {
							$bg.fadeOut(2000);
							setTimeout(rf.layout.loadImages, 2100);
						}, 30000);
					});
					setTimeout(rf.layout.loadImages, 100);
				}

				rf.layout.initNoneDisplayContent();
				rf.layout.initToolTip();
				rf.layout.initNiceScroll();
				rf.layout.initLightBox();
				rf.layout.initMixitup();
			},
			
			initNoneDisplayContent: function () {
				var $element = $(rf.layout.settings.elements.mainMenu);
				if ($element) {
					$element.fadeIn(1000);
				}
				$element = $(rf.layout.settings.elements.userPanel);
				if ($element) {
					$element.fadeIn(1500);
				}
			},

			initToolTip: function () {
				var $menuItem = $(rf.layout.settings.elements.mainMenuItem);
				if ($menuItem) {
					$menuItem.tooltip({
						placement: 'top'
					});
				}

				$menuItem = $(rf.layout.settings.elements.panelMenuItem);
				if ($menuItem) {
					$menuItem.tooltip({
						placement: 'top'
					});
				}
			},

			initMixitup: function () {
				var $grid = $(rf.layout.settings.elements.grid);
				if ($grid) {
					$grid.mixitup({
						effects: ['fade', 'grayscale'],
						easing: 'snap',
						transitionSpeed: 800
					});
				}
			},

			initNiceScroll: function () {
				var $htmlElem = $("html");
				if ($htmlElem) {
					$htmlElem.niceScroll({
						cursorcolor: '#a71e2b',
						cursorborder: 0,
						zindex: 99999,
					});
				}
			},

			initLightBox: function () {
				var $lightbox = $(rf.layout.settings.elements.lightbox);
				if ($lightbox) {
					$lightbox.lightbox();
				}
			},

			loadImages: function () {
				var images = rf.layout.settings.vars.images;

				if (images) {
					/* Select a random image number and make sure this is not equal to the previous image */
					while (rf.layout.settings.vars.imagePrev === rf.layout.settings.vars.rndPrev) {
						rf.layout.settings.vars.rndPrev = Math.floor(Math.random() * images.length);
					}
					/* Show the selected image */
					rf.layout.loadImage(rf.layout.settings.vars.rndPrev);
					rf.layout.settings.vars.imagePrev = rf.layout.settings.vars.rndPrev;
				}
			},

			loadImage: function (imageNum) {
				var images = rf.layout.settings.vars.images;
				var $bg = $(rf.layout.settings.elements.bgImage);

				if ($bg && images && !isNaN(imageNum) && imageNum < images.length) {
					$bg.attr("src", images[imageNum]);
				}
			},
		};
})();